class NewsController < BaseController 

  # 新闻列表页
  def index
    get_public_var

    condition = {}
    if @important
      condition = {is_important_news: @important}
    elsif @category_id.present?
      condition={category_id: get_categories }
    elsif params["sptitle_id"].present?
      condition = {id: get_sptitles}
    end  


    get_right_news
    get_right_sptitles
    get_right_tshl
    meiti = Category.where(:cataname => '媒体华理').first
    if @category_id.present? &&  @category_id.to_i == meiti.id
      @news = News.where(:category_id => meiti.id).where(:enable =>true).order("pubtime desc").page params[:page]  #.where(check:1)
      @news_count = News.where(:category_id => meiti.id).where(:enable =>true).size #.where(check:1)
   
    else
      @news_count = News.index_news.where(condition).where(:enable =>true).size
      @news = News.index_news.where(condition).where(:enable =>true).order("ordinal desc").order("pubtime desc").page params[:page]
    end

  end

  # 新闻最终页 
  def show    
    get_public_var
  	@news = News.where({id: params[:id]}).first
    @news.update_attributes(hits: 1) if @news.hits.blank?
    sql = "update news set hits = hits+1 where id=#{@news.id}"
    ActiveRecord::Base.connection.execute(sql)
  	if @news.blank?
  		redirect_to "/404" 
  		return
  	else
  	  keywords = @news.keywords
  	  keys = keywords.split('|')


  	  if keys.blank?
   	 	 @similar = News.index_news.where({category_id: @news.category_id}).order("pubtime desc").limit(10)
	  else
		 @similar = News.index_news.where({category_id: @news.category_id}).where(["keywords like ?", "%"+keys[0]+"%"]  ).order("pubtime desc").limit(10)
      		if @similar.size()==0
       			@similar = News.index_news.where({category_id: @news.category_id}).order("pubtime desc").limit(10)	
      		end
          end
  	end
  end
  
  


  private
  def index_recommend
    huali = Category.where(:cataname => '媒体华理')
    @huali = News.where(category_id: huali.id).order("created_at desc").limit(5)
    @important = News.index_news.where(:is_important_news => 1).limit(12)
  end

  def get_public_var
    @important = params["important"]
    @category_id = params["category_id"]
    @sptitle_id = params["sptitle_id"]    
    @category = Category.where(id: @category_id).first
  end

  def get_categories
    categories = Array.new()
    categories << @category.id
    tmp_categories = NewsHelper::CATEGORY_HASH_ARRAY[@category.id.to_s]
    categories << tmp_categories if tmp_categories.present?
    categories.each do |category_array|
      if category_array.class == Array
        category_array.each do |category_id|
          category = Category.find category_id
          categories << category_id
          category_children = category.children.collect(&:id)
          categories << category_children if category_children.present? 
        end
      elsif category_array.class == Fixnum
        category = Category.find category_array
        category_children = category.children.collect(&:id)
        categories << category_children if category_children.present? 
      end
    end

    categories

  end

  def get_sptitles
    @sptitle = Sptitle.find(params["sptitle_id"])
    ids = NewsSptitle.where(sptitle_id: @sptitle.id).collect(&:news_id)    
  end

  def get_right_news
    #if @important == '1'
    #  important = 0
    #else
      important = 1
    #end
    @intro_news = News.index_news.where(is_important_news: important).where("pubtime>=?",30.days.ago).order("ordinal desc").order("hits desc").limit(10)
  end

  def get_right_sptitles
    @hot_sptitles = Sptitle.all.order("ordinal desc").order("created_at desc").limit(2)
  end

  def get_right_tshl
    @pic_say_huali_news = News.index_news.where(category_id: NewsHelper::TSHL_CATEGORY).order("created_at desc").limit(5)
  end

end
